<template>
  <!-- 搜索结果页-头部导航 -->
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->

    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <!-- 列表 -->
    <div class="scroll-box">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <article-list-item
          v-for="item in list"
          :key="item.art_id"
          :item="item"
          @click.native="goDetail(item.art_id)"
        />
      </van-list>
    </div>
  </div>
</template>

<script>

import { getSearchResultAPI } from '@/api'
import ArticleListItem from '../Home/components/article-list-item.vue'
export default {
  name: 'SearchResult',
  components: {
    ArticleListItem
  },
  data () {
    return {
      params: {
        page: 1,
        perPage: 10,
        q: ''
      },
      list: [],
      loading: false,
      finished: false
    }
  },
  mounted () {
    this.fetchList()
  },
  methods: {
    async fetchList () {
      this.params.q = this.$route.query.q
      const res = await getSearchResultAPI(this.params)
      this.list = res.data.results
    },
    goDetail () { },
    async onLoad () {
      // 上拉业务逻辑
      // 1. 获取下一页的数据
      this.params.page++
      const res = await getSearchResultAPI(this.params)
      // 2. 新老数据列表拼接
      this.list = [...this.list, ...res.data.results]
      // 3. 把loading从true设置为false关闭
      this.loading = false
      // 4. 判断是否还有数据
      if (res.data.results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
.scroll-box {
  position: fixed;
  top: 46px;
  bottom: 0;
  width: 100%;
  overflow: auto;
}
</style>
